﻿using System;
using System.Diagnostics;
using System.Linq;
using Mvvm;
using LayoutPlanning.ViewModel;

namespace LayoutPlanning.Model
{
    [Serializable]
    public class GeneralTask : BindableBase
    {
        public enum WorkTaskType
        {
            Carry,
            Work,
            Walk,
            General
        }

        private Product _carryObject;
        private GraficalObject _fromStation;
        private WorkPosition _fromWorkPosition;
        private string _taskName;
        private TimeSpan _taskTime;
        private WorkTaskType _taskType = WorkTaskType.General;
        private GraficalObject _toStation;
        private WorkPosition _toWorkPosition;


        public GeneralTask(WorkTaskType outp, Station taskFromStation)
        {
            TaskType = outp;
            TaskFromStation = taskFromStation;
        }

        public Product TaskCarryObject
        {
            get { return _carryObject; }
            set
            {
                _carryObject = value;
                RaisePropertyChanged();
            }
        }

        public WorkTaskType TaskType
        {
            get { return _taskType; }
            set
            {
                if (value == _taskType)
                    return;
                _taskType = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// Gets and sets the value for FromStation
        /// </summary>
        public GraficalObject TaskFromStation
        {
            get { return _fromStation; }
            set
            {
                if (value == _fromStation)
                    return;
                _fromStation = value;
                TaskFromWorkPosition = (_fromStation as Station)?.WorkPositions.FirstOrDefault();
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// Gets and sets the value for TaskFromWorkPosition
        /// </summary>
        public WorkPosition TaskFromWorkPosition
        {
            get { return _fromWorkPosition; }
            set
            {
                if (value == _fromWorkPosition)
                    return;
                _fromWorkPosition = value;
                RaisePropertyChanged();
            }
        }


        /// <summary>
        /// Gets and sets the value for FromStation
        /// </summary>
        public GraficalObject TaskToStation
        {
            get { return _toStation; }
            set
            {
                if (value == _toStation)
                    return;
                _toStation = value;

                TaskToWorkPosition = (_toStation as Station)?.WorkPositions.FirstOrDefault();
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// Gets and sets the value for TaskFromWorkPosition
        /// </summary>
        public WorkPosition TaskToWorkPosition
        {
            get { return _toWorkPosition; }
            set
            {
                if (value == _toWorkPosition)
                    return;
                _toWorkPosition = value;
                RaisePropertyChanged();
            }
        }

        public TimeSpan TaskTime
        {
            get { return _taskTime; }
            set
            {
                if (value == _taskTime)
                    return;
                _taskTime = value;
                RaisePropertyChanged();
            }
        }

        public string TaskName
        {
            get { return _taskName; }
            set
            {
                if (value == _taskName)
                    return;
                _taskName = value;
                RaisePropertyChanged();
            }
        }

        public override string ToString()
        {
            return "";
        }

        public static GeneralTask CreateTask(WorkTaskType outp, Station taskFromStation)
        {
            switch (outp)
            {
                case WorkTaskType.Walk:
                    return new WalkTask(taskFromStation);
                case WorkTaskType.Work:
                    return new WorkTask(taskFromStation);
                case WorkTaskType.Carry:
                    return new CarryTask(taskFromStation);
                case WorkTaskType.General:

                default:
                    return new GeneralTask(outp, taskFromStation);
            }
        }
    }
    [Serializable]
    public class WalkTask : GeneralTask
    {
        public WalkTask(Station taskFromStation) : base(WorkTaskType.Walk, taskFromStation)
        {
        }

        public WalkTask(Station toStation, Station fromStation, string taskName) : base(WorkTaskType.Walk, fromStation)
        {
            TaskToStation = toStation;
            TaskName = taskName;
        }
    }
    [Serializable]
    public class WorkTask : GeneralTask
    {
        public WorkTask(Station taskFromStation) : this(taskFromStation, "", TimeSpan.Zero)
        {
        }

        public WorkTask(Station taskFromStation, string taskName, TimeSpan taskTime)
            : base(WorkTaskType.Work, taskFromStation)
        {
            TaskToStation = taskFromStation;
            TaskName = taskName;
            TaskTime = taskTime;
        }
    }
    [Serializable]
    public class CarryTask : GeneralTask
    {
        public CarryTask(Station taskFromStation) : base(WorkTaskType.Carry, taskFromStation)
        {
        }

        public CarryTask(Station taskFromStation, string taskName) : base(WorkTaskType.Carry, taskFromStation)
        {
            TaskToStation = taskFromStation;
            TaskName = taskName;
        }
    }
}